package com.easy.service.impl;

import com.easy.bean.Purchase_order;
import com.easy.mapper.PurchaseOrderDao;
import com.easy.service.PurchaseOrderService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {

    @Autowired
    PurchaseOrderDao purchaseOrderDao;

    @Override
    public Purchase_order add(Purchase_order order) {
        // 验证必要字段
        if (order.getQuantity() == null || order.getQuantity() <= 0) {
            throw new IllegalArgumentException("采购数量必须大于0");
        }
        
        if (order.getSupplierId() == null) {
            throw new IllegalArgumentException("供应商ID不能为空");
        }
        
        if (order.getMaterialId() == null) {
            throw new IllegalArgumentException("材料ID不能为空");
        }
        
        if (order.getIsDeleted() == null) {
            order.setIsDeleted((byte)0); // 默认未删除
        }
        
        purchaseOrderDao.add(order);
        return purchaseOrderDao.one(order.getId());
    }

    @Override
    public int del(Integer id) {
        return purchaseOrderDao.del(id);
    }

    @Override
    public Purchase_order edit(Purchase_order order) {
        purchaseOrderDao.edit(order);
        return purchaseOrderDao.one(order.getId());
    }

    @Override
    public List<Purchase_order> all(Integer supplierId, Integer materialId) {
        return purchaseOrderDao.all(supplierId, materialId);
    }

    @Override
    public Purchase_order one(Integer id) {
        return purchaseOrderDao.one(id);
    }

    @Override
    public void page(Page page, Purchase_order order) {
        int count = purchaseOrderDao.pageCount(order);
        List<Purchase_order> data = purchaseOrderDao.page(page, order);
        page.setCount(count);
        page.setData(data);
    }
}
